package us.gaaoc.framework.dao;

import java.util.UUID;

import org.hibernate.Query;

import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonHashedSSN;

public class PersonHashedSSNDAOHibernate extends GenericHibernateDAO<PersonHashedSSN, String, PersonHashedSSNDAO> implements PersonHashedSSNDAO {

	public Person findPersonByHashedSSN(String ssn) {
		Query q = this.getSession().createQuery("from PersonHashedSSN phssn where phssn.hashedSSN = ?");
		q.setString(0, ssn);
		LOG.debug("Inside the findPersonByHashedSSN in the  PersonHashedSSNDAOHibernate");
		/*
		//String personUuid = (String)((PersonHashedSSN)q.uniqueResult()).getPersonUuid();
		if((PersonHashedSSN)q.uniqueResult() != null){
			PersonDAO personDAO = DAOFactory.DEFAULT.getPersonDAO(); 			
			return personDAO.findPersonByUuid((String)((PersonHashedSSN)q.uniqueResult()).getPersonUuid());
		}else {
			return null;
		}
		*/
		
		
		
		
		// GAJETWOX-146 - We can't assume hashedSSN is unique because the 
		// database column has no unique constraint.
		if(q.list().size() != 0){
			LOG.debug("The person is in the data base already");
			PersonDAO personDAO = DAOFactory.DEFAULT.getPersonDAO(); 			
			return personDAO.findPersonByUuid((String)((PersonHashedSSN)q.list().get(0)).getPersonUuid());
		}else {
			LOG.debug("The person is not in the data base ");
			return null;
		}

	}

	public void insertIntoPersonIdHashedSSN(String personUuid, String hashedSSN) {
		PersonHashedSSN phssn = new PersonHashedSSN();
		phssn.setPersonUuid(personUuid);
		phssn.setHashedSSN(hashedSSN);
		phssn.setUuid(UUID.randomUUID().toString());
		this.makePersistent(phssn);
		
		
	}


	
}
